/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_out.js                            
*  Description  : 마이홈 탈퇴
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-14  김효순    Created.  
*  2012-05-14  김효순    Complete.
*******************************************************************************/
jQuery(function($){
	
	// 서버통신
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		, withdrawSubmit: function(arg){
			var thisClass = this;
			
//			arg = {
//				"강제": null
//			}
			
			//ajax
			var serviceName = "delete";
			var options = {
				"withdrawalType": arg["강제"]
			}
			document.objGlobal.ajax({
				"sid":"US001",
				"servicename":serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishWithdrawSubmit(res);
			});
			/*setTimeout(function(){
				var rnd = document.objGlobal.random(0, 1);
				var ret = {
					"결과코드": rnd
					, "메시지": null
				}
				document.objControl.finishWithdrawSubmit(ret);
			}, 1500);*/
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		
		this.checkAuth = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
			
			this.checkAuth = _util.getCookie("check_auth");
			_util.delCookie("check_auth", "/");
			
			if(this.checkAuth != "Y"){
				_util.executeThread(function(){
					return _gf.getMyInfoData();
				}, function(){
					document.location.href = _ROOT + "/mypage/my_info_pw.html";
				});
				
				return;
			}
			
			if(!_util.getCookie("member_id")) document.location.href = "/index";
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			$.ajax({
				url: _ROOT + "/js/mypage_common.js"
				, dataType: "script"
				, async: false
				, success: function(){
					
				}
				, error: function(){
					alert("error");
				}
			});
			
			_util.executeThread(function(){
				for(var k in thisClass._beforeStart){
					if(!thisClass._beforeStart[k]) return false;
				}
				
				return (_gf.getMyInfoData());
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			// 전송
			$("div#container").on("click", "img[alt='탈퇴요청']", function(){
				if(!confirm("탈퇴 하시겠습니까?")) return;
				
				thisClass.withdraw();
			});
		}
		, _start: function(){
			
		}
		// 전송완료
		, finishWithdrawSubmit: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			
			_gf.spinEnd();
			switch(ret["결과코드"]){
				case "00000":
					_gf.logout();
					break;
				case "E0001":
					var winConfirm = window.open("../popup/pop_my_out.html", "winConfirm", "width=400, height=100, scrollbars=0");
					if(winConfirm) winConfirm.focus();
					break;
			}
		}
		// 탈퇴진행
		, withdraw: function(){
			var win = arguments[0];
			var n = arguments[1];
			
			if(win) win.close();
			
			_gf.spinStart("sending...");

			this.objComm.withdrawSubmit({
				"강제": ((n) ? "Y":"N")
			});
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});
